package com.lazy;

/**
 * 双重检查锁
 * <p>
 * 优点；性能提高了，线程安全了
 * 缺点：可读性差，不够优雅
 *
 * @author: cong zhi
 * @createDate: 2022/5/7 8:48
 * @updateUser: cong zhi
 * @updateDate: 2022/5/7 8:48
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton instance = null;

    private LazyDoubleCheckSingleton() {
    }

    public static LazyDoubleCheckSingleton getInstance() {
        // 检查是否要阻塞
        if (null == instance) {
            synchronized (LazyDoubleCheckSingleton.class) {
                // 检查是否需要重新创建实例
                if (null == instance) {
                    instance = new LazyDoubleCheckSingleton();
                    // 指令重排序的问题
                }
            }
        }
        return instance;
    }
}
